<?php

class Core
{
    private static $config             = null;
    private static $dbConn             = null;
    private static $resourceManager    = null;
    private static $themeSubstitutions = null;
    private static $substitutions      = null;
    private static $cache              = null;
    private static $props              = null;
    private static $testimonialsSubstitutions = null;
    private static $app                = null;

    private function __construct() {}

    public static function Config()
    {
        if ( is_null(self::$config) )
        {
            self::$config = new Config( CONFIG_FILE, self::Cache() );
        }
        return self::$config;
    }

    public static function SetConfig(Config $config)
    {
        self::$config = $config;
    }

    public static function CreateApplication( $name = 'site' )
    {
        return new WebApplication($name);
    }

    public static function SetApplication( $app )
    {
        if ( self::$app === null || $app === null )
        {
            self::$app = $app;
        }
        else
        {
            throw new Exception('Application can only be created once.');
        }
    }

    public static function ThemeSubstitutions()
    {
        if ( is_null(self::$themeSubstitutions) )
        {
            self::$themeSubstitutions = new ThemeSubstitutions();
        }
        return self::$themeSubstitutions;
    }

    /**
     * @return WebApplication
     */
    public static function App()
    {
        return self::$app;
    }

    /**
     * @return iSpringResourceManager
     */
    public static function ResourceManager()
    {
        if ( is_null(self::$resourceManager) )
        {
            self::$resourceManager = new iSpringResourceManager( self::Cache(), self::Substitutions() );
        }
        return self::$resourceManager;
    }

    /**
     * @return ICache
     */
    public static function Cache()
    {
        if ( is_null(self::$cache) )
        {
            if ( CACHE == ICache::E_ACCELERATOR && function_exists('eaccelerator_info') )
            {
                self::$cache = new eAcceleratorCache( CACHE_DIR . "ea_cache" );
            }
            else if ( CACHE == ICache::FILE )
            {
                self::$cache = new FileCache( CACHE_DIR );
            }
            else
            {
                self::$cache = new NullCache();
            }
        }
        return self::$cache;
    }

    /**
     * Returns DBConnection.
     *
     * @return DBConnection
     */
    public static function DBConn()
    {
        if ( is_null(self::$dbConn) )
        {
            $params = self::Config()->dbconn;
            self::$dbConn = $dbConn = new DBConnection();

            $port = null;
            $socket = null;

            $pos = strpos( $params->host, ":" );
            if ( $pos !== false )
            {
                $port = substr( $params->host, $pos + 1 );
                if ( !is_numeric( $port ) )
                {
                    $socket = $port;
                    $port = null;
                }
                $params->host = substr( $params->host, 0, $pos );
            }

            $dbConn->Connect( $params->host, $params->login, $params->password, $port, $socket );
            $dbConn->SelectDB( $params->database );
            $dbConn->Query( "SET CHARACTER SET utf8" );
            $dbConn->Query( "SET NAMES 'utf8'" );
        }
        return self::$dbConn;
    }

    public static function SetSubstitutions($substitutions)
    {
        self::$substitutions = $substitutions;
    }

    public static function Substitutions()
    {
        if (is_null(self::$substitutions))
        {
            return new DummySubstitutions();
        }
        return self::$substitutions;
    }

    public static function TestimonialsSubstitutions()
    {
        if ( is_null(self::$testimonialsSubstitutions) )
        {
            self::$testimonialsSubstitutions = new TestimonialsSubstitutions();
        }
        return self::$testimonialsSubstitutions;
    }

    /**
     * @return TemplateEngine
     */
    public static function CreateTemplateEngine( $path = PATH_TEMPLATES )
    {
        return new TemplateEngine( $path );
    }

    public static function GetProperty( $propName, $defValue = null )
    {
        self::LoadProperties();
        if ( isset(self::$props[$propName]) )
        {
            return self::$props[$propName];
        }
        return $defValue;
    }

    public static function SetProperty( $propName, $value )
    {
        self::LoadProperties();
        self::$props[$propName] = $value;
        self::SaveProperties();
    }

    public static function GetStatisticsBaseUrl()
    {
        return (Request::IsSecure() ? 'https://' : 'http://') . self::Config()->statistics->base_url . '/';
    }

    public static function GetContentServerUrl()
    {
      
        return (Request::IsSecure() || self::Config()->server_url->use_cdn === "false") ? Request::GetUriPrefix() : self::Config()->server_url->content_server_url;
    }

    /**
     * Returns site language.
     * @return string
     */
    public static function GetLanguage()
    {
        return LanguageConfig::GetLang();
    }

    private static function LoadProperties()
    {
        if ( is_null(self::$props) )
        {
            $props = self::Cache()->Get("properties");
            if ( is_null($props) || !is_array($props) )
            {
                $props = array();
            }
            self::$props = $props;
        }
        return self::$props;
    }

    private static function SaveProperties()
    {
        self::Cache()->Set("properties", self::$props);
    }
}

?>
